package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     */
    @Log
    @PostMapping
    public Result addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 修改角色信息
     */
    @Log
    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 根据ID查询角色信息
     */
    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable("id") Integer id) {
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }

    /**
     * 查询所有角色
     */
    @GetMapping("/list")
    public Result getAllRoles() {
        List<Role> roles = roleService.getAllRoles();
        return Result.success(roles);
    }

    /**
     * 条件分页查询角色列表
     */
    @GetMapping
    public Result getRoles(String name, String label,
                           @RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<Role> rolePage = roleService.getRoles(name, label, page, pageSize);
        return Result.success(rolePage);
    }

    /**
     * 删除角色信息
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        roleService.deleteRole(id);
        return Result.success();
    }
}